<template>
  <label
    :class="[
      'sq-checkbox',
      isChecked ? 'is-checked' : '',
      disabled ? 'is-disabled' : '',
    ]"
  >
    <span class="sq-checkbox__input">
      <span
        class="sq-checkbox__inner"
        :style="{
          backgroundColor: isChecked ? activeColor : '#fff',
          borderColor: isChecked ? activeColor : '#dcdfe6',
        }"
      ></span>
      <input
        type="checkbox"
        class="sq-checkbox__original"
        :name="name"
        :value="label"
        v-model="model"
        :disabled="disabled"
      />
    </span>
    <span class="sq-checkbox__label">
      <slot></slot>
    </span>
  </label>
</template>

<script>
export default {
  name: "SqCheckbox",
  // 接收父组件checkboxgroup的this,如果有
  inject: {
    CheckboxGroup: {
      default: "",
    },
  },
  data() {
    return {};
  },
  methods: {},
  components: {},
  props: {
    // 父组件传来的值，Boolean
    value: {
      type: Boolean,
      default: false,
    },
    // 父组件传进来的label，String类型，作为子组件元素input的value值
    label: {
      type: String,
      default: "",
    },
    // 父组件传的name，String类型，用作子组件的name
    name: {
      type: String,
      default: "",
    },
    disabled: {
      type: Boolean,
      default: false,
    },
    activeColor: {
      type: String,
      default: "#409eff",
    },
  },
  created() {},
  mounted() {},
  computed: {
    model: {
      get() {
        return this.isGroup ? this.CheckboxGroup.value : this.value;
      },
      set(value) {
        this.isGroup
          ? this.CheckboxGroup.$emit("input", value)
          : this.$emit("input", value);
      },
    },
    isGroup() {
      return !!this.CheckboxGroup;
    },
    isChecked() {
      return this.isGroup ? this.model.includes(this.label) : this.model;
    },
  },
  watched: {},
};
</script>

<style scoped lang="less">
@import "./index.less";
</style>
